
**********
* Readme *
**********

* This script estimates potential association between rho lower bound and individual atributes.


* Root folder (PATH TO BE DEFINED BY THE USER)
**********************************************
clear all
global analysis "C:/***/replication_package"


* Timestamped log
*****************
global today = strofreal(date(c(current_date), "DMY"), "%tdYYNNDD")
log using "${analysis}/code/logs/6_rho_analysis_${today}.smcl", replace


***************
* Import data *
***************

use "${analysis}/data/2_2_pof_clean.dta", clear

* Calculate top decile of basic expenses
gen share_fam_exp_educ     = fam_exp_educ / fam_exp_tot
gen share_fam_exp_pers     = fam_exp_pers / fam_exp_tot
gen share_fam_exp_housing  = fam_exp_housing / fam_exp_tot
gen share_fam_exp_medicine = fam_exp_medicine / fam_exp_tot
gen share_fam_exp_food     = fam_exp_food / fam_exp_tot

label var share_fam_exp_educ     "Schooling expenses / total"
label var share_fam_exp_pers     "Personal expenses / total"
label var share_fam_exp_housing  "Housing expenses / total"
label var share_fam_exp_medicine "Medicine expenses / total"
label var share_fam_exp_food     "Food expenses / total"

svyset psu_id [pweight = pweight], strata(strata_id) singleunit(centered)
epctile share_fam_exp_educ, percentiles(50 90) svy
epctile share_fam_exp_pers, percentiles(50 90) svy
epctile share_fam_exp_housing, percentiles(50 90) svy
epctile share_fam_exp_medicine, percentiles(50 90) svy
epctile share_fam_exp_food, percentiles(50 90) svy

gen top_dec_educ     = (share_fam_exp_educ > .147)
gen top_dec_pers     = (share_fam_exp_pers > .127)
gen top_dec_housing  = (share_fam_exp_housing > .581)
gen top_dec_medicine = (share_fam_exp_medicine > .09)
gen top_dec_food     = (share_fam_exp_food > .346)

label var top_dec_educ     "Education expenses > 15% of total"
label var top_dec_pers     "Personal expenses > 13% of total"
label var top_dec_housing  "Housing expenses > 58% of total"
label var top_dec_medicine "Medicine expenses > 9% of total"
label var top_dec_food     "Food expenses > 35% of total"


*************
* Merge rho *
*************

merge 1:1 ind_id using "${analysis}/data/5_est_boot_rho.dta", nogenerate keepusing(rho_avg avg_r)

* Rescale variables for easier interpretation
replace rho_avg = rho_avg * 100 // lower bound rate rate in %
replace avg_r = avg_r * 100 // average market rate in %

* Estimated rho
svyset psu_id [pweight = pweight], strata(strata_id) singleunit(centered)
svy: mean rho_avg
epctile rho_avg, percentiles(10 20 30 40 50 60 70 80 90) svy

* Share of constrained oaw (baseline)
gen constrained = (rho_avg > avg_r) if work_state == "Own-account worker":work_state
svy: mean constrained if work_state == "Own-account worker":work_state

* Other auxiliary variables
gen fam_net_inc_pc_ex_me_000 = fam_net_inc_pc_ex_me / 1000
label var fam_net_inc_pc_ex_me_000 "Family per cap inc ex my work inc"

gen fam_net_inc_pc_ex_me_000_2 = fam_net_inc_pc_ex_me_000^2
label var fam_net_inc_pc_ex_me_000_2 "(Family per cap inc ex my work inc)^2"
 
 
************************
* Groups of covariates *
************************

* Baseline
unab base_all : rg_* ae_* at_* region_*
unab ref_groups : rg_1_nonwhite_female ae_1_1 region_SP_c
global baseline : list base_all - ref_groups

* Family
unab family_all : fp_* n_kids n_youngs n_adults n_seniors
unab family_ref_groups : fp_h_wp_nk
global family : list family_all - family_ref_groups

* Credit
global no_credit no_savings_account no_overdraft_facility no_credit_card

* Other family income
global other_income fam_net_inc_pc_ex_me_000 fam_net_inc_pc_ex_me_000_2

* General self-evaluation of finacial conditions
global eval_finance i.ends_meet

* Non-essencial consumption
global extra_expenses top_dec_educ top_dec_pers

* Essential consumption
global basic_expenses top_dec_housing top_dec_medicine top_dec_food

* Access to food
global food i.food_insec

* Access to clothing
global eval_clothing bad_cond_clothing 

* Housing precarity
global house_cond people_per_sleep_room house_pest house_dampness 

* All
global all $baseline $family $no_credit $other_income $eval_finance $extra_expenses $basic_expenses $house_cond $eval_clothing $food 


****************************************************************************
* Descriptive associations between estimated rho and individual attributes *
****************************************************************************

eststo model_A: regress rho_avg $baseline $family $no_credit $other_income [pweight = pweight], vce(cluster psu_id)
eststo model_B: regress rho_avg $baseline $family $eval_finance $extra_expenses $basic_expenses [pweight = pweight], vce(cluster psu_id)
eststo model_C: regress rho_avg $baseline $family $house_cond $eval_clothing $food [pweight = pweight], vce(cluster psu_id)
eststo model_D: regress rho_avg $all [pweight = pweight], vce(cluster psu_id)


* Results
*********
esttab model_A model_B model_C model_D, /// 
  nonumbers nobaselevels mlabels(, titles) nobaselevels cells("b(fmt(3)) se(par fmt(3))") collabel("coeff." "s.e.") /// 
  drop($baseline $family _cons) ///  
		refcat( ///
		  no_savings_account "Lack of access to financial services" ///
		  fam_net_inc_pc_ex_me_000 "Income from other sources (in R{c $|} 1 000)" ///
		  2.ends_meet "Making ends meet (ref: very easy)" ///
		  top_dec_educ "Large non-essential expenses (top decile)" ///
		  top_dec_housing "Large essential expenses (top decile)" ///
		  2.food_insec "Food insecurity (ref: no insecurity)" ///
    bad_cond_clothing "Clothing precarity (ref: good, adequate)" ///
    people_per_sleep_room "Housing precarity", nolabel) ///
		stats(r2_a N, ///
		  labels("Adjused R2" "Number of observations") ///
		  fmt(3 %9.0fc))

          
* TABLE 8
*********
esttab model_A model_B model_C model_D using "${analysis}/results/tables/table8.tex", style(tex) fragment replace ///
  nonumbers mgroups(none) mlabels(none) eqlabels(none) collabel(none) nobaselevels cells("b(fmt(2)) se(par fmt(2))") label /// 
  drop($baseline $family _cons) ///  
  refcat( ///
    no_savings_account          "[nospace]\textit{Lack of access to financial services}" ///
    fam_net_inc_pc_ex_me_000    "[nospace]\textit{Income from other sources (in R\\$ 1,000)}" ///
    2.ends_meet                 "[nospace]\textit{Making ends meet (ref: very easy)}" ///
    top_dec_educ                "[nospace]\textit{Has large non-essential expenses (top decile)}" ///
    top_dec_housing             "[nospace]\textit{Has large essential expenses (top decile)}" ///
    2.food_insec                "[nospace]\textit{Food insecurity (ref: no insecurity)}" ///
    bad_cond_clothing           "[nospace]\textit{Clothing precarity (ref: good, adequate)}" ///
    people_per_sleep_room       "[nospace]\textit{Housing precarity}", nolabel) ///
  varlabels(, /// 
    prefix(\hspace{2ex}) ///
    elist(no_credit_card \tabularnewline fam_net_inc_pc_ex_me_000_2 \tabularnewline 6.ends_meet \tabularnewline top_dec_pers \tabularnewline top_dec_food \tabularnewline house_dampness \tabularnewline bad_cond_clothing \tabularnewline 4.food_insec \tabularnewline)) ///
  stats(r2_a N, ///
    labels("\midrule\textit{Model statistics} \\ \hspace{2ex}Adjusted R^2" "\hspace{2ex}Number of observations") ///
    fmt(3 %9.0fc)) ///
  substitute("%" "\%" ///
             ">" "$>$" ///
             "^2" "$^2$" ///
             "\hline" "" ///
             "&" " & " "  " " " "  " " " "  " " " "  " " " ///
             " & & " " & $\cdot$ & " ///
             " & & " " & $\cdot$ & " ///
             " & & " " & $\cdot$ & " ///
             " & \\" " & $\cdot$ \\" ///
             "\hspace{2ex}[nospace]" "" ///
             "& $\cdot$ & $\cdot$ & $\cdot$ & $\cdot$ & $\cdot$ & $\cdot$ & $\cdot$ & $\cdot$ \\" "& & & & & & & & \\")  
  
    
* FIGURE 4
**********

coefplot model_D, ///
  keep($no_credit $other_income *.ends_meet $extra_expenses $basic_expenses $house_cond $eval_clothing *.food_insec) ///
  graphregion(margin(l-14)) ///
  plotregion(margin(b+1 t+1 l-1 r-1)) ///
  mcolor(gs4) msymbol(O) mfcolor(white) msize(vsmall) ///
  ciopts(lcolor(gs4)) ///
  coeflabels(, notick labgap(2) labsize(small)) ///
  xlabel(-4 (4) 17, labsize(small)) ///
  xline(0, lw(medium) lc(gs14) noextend) ///
  grid(glcolor(gs14) glpattern(solid) glwidth(medium) nogextend) ///
  headings(no_savings_account = "{bf:Lack of access to financial services}" ///
           fam_net_inc_pc_ex_me_000 = "{bf:Income from other sources (in R{c $|} 1,000)}" ///
           top_dec_educ = "{bf:Has large non-essential expenses (top decile)}" ///
           top_dec_housing = "{bf:Has large essential expenses (top decile)}" ///
           2.ends_meet = "{bf:Making ends meet (ref: very easy)}" ///
           2.food_insec = "{bf:Food insecurity (ref: no insecurity)}" ///
           bad_cond_clothing = "{bf:Clothing precarity (ref: good, adequate)}" ///
           people_per_sleep_room = "{bf:Housing precarity}", labgap(2) labsize(small)) ///
  xtitle("{bf:Marginal association with}" "{bf:discount rate lower bound}" "{bf:(in percent per month)}", linegap(.5) size(small)) ///
  ysize(8) xsize(5)

graph export "${analysis}/results/figures/fig4.eps", replace

    
* End of script
***************
cap log close